package com.lib.server.config;

import com.lib.common.interceptor.TokenAdminInterceptor;
import com.lib.common.interceptor.TokenUserInterceptor;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
@Slf4j
public class WebConfig extends WebMvcConfigurationSupport {
    @Autowired
    private TokenAdminInterceptor tokenAdminInterceptor;
    @Autowired
    private TokenUserInterceptor tokenUserInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器....");
        registry.addInterceptor(tokenAdminInterceptor)
                .addPathPatterns("/admin/**")//拦截所有的请求
                .excludePathPatterns("/admin/login");
//                .excludePathPatterns("/admin/dish/page")
//                .excludePathPatterns("/admin/dish")
//                .excludePathPatterns("/admin/dish/status/{status}")
//                .excludePathPatterns("/admin/common/upload")
//                .excludePathPatterns("/admin/dish/{id}")
//                .excludePathPatterns("/admin/dish")
//                .excludePathPatterns("/admin/category/status/{status}")
//                .excludePathPatterns("/admin/category/categoryId")
//                .excludePathPatterns("/admin/category/id")
//                .excludePathPatterns("/admin/category/ids")
//                .excludePathPatterns("/admin/category")
//                .excludePathPatterns("/admin/category/page");
        registry.addInterceptor(tokenUserInterceptor)
                .addPathPatterns("/user/**")//拦截所有的请求
                .excludePathPatterns("/user/login");
//                .excludePathPatterns("/user/categories/categoryName");
    }
    @Bean
    public OpenAPI libraryOpenAPI() {
        return new OpenAPI()
                .info(new Info()
                        .title("图书管理系统接口文档")
                        .version("1.0")
                        .description("API 文档"));
    }
    // 新增：用户模块 API 分组
    @Bean
    public GroupedOpenApi userApi() {
        return GroupedOpenApi.builder()
                .group("用户模块")
                .pathsToMatch("/user/**")
                .build();
    }

    // 新增：管理员模块 API 分组
    @Bean
    public GroupedOpenApi adminApi() {
        return GroupedOpenApi.builder()
                .group("管理员模块")
                .pathsToMatch("/admin/**") // 可添加多个路径模式
                .build();
    }
    //设置静态资源映射 --- 这个方法是重写父类的方法
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开始设置静态资源...");
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}
